using System;
using System.Collections.Generic;
using System.Text;

namespace SharpMud.MudLib.NovaSMAUG.Interop
{
    public class ArrayIndexPointer<T> : ICloneable
    {
        private T[] _BS_Array;
        private int _BS_Index;

        private ArrayIndexPointer() { }

        public T Value
        {
            get
            {
                return _BS_Array[_BS_Index];
            }
            set
            {
                _BS_Array.SetValue(value, _BS_Index);
            }
        }

        public int Index
        {
            get
            {
                return _BS_Index;
            }
            set
            {
                if (value < 0)
                    throw new IndexOutOfRangeException();
                if (value >= _BS_Array.Length)
                    throw new IndexOutOfRangeException();
                _BS_Index = value;
            }
        }

        public static ArrayIndexPointer<T> Create(T[] array)
        {
            return Create(array, 0);
        }
        public static ArrayIndexPointer<T> Create(T[] array, int index)
        {
            if (index < 0 || index >= array.Length)
                throw new IndexOutOfRangeException();
            ArrayIndexPointer<T> n = new ArrayIndexPointer<T>();
            n._BS_Array = array;
            n._BS_Index = index;
            return n;
        }

        #region ICloneable Members

        object ICloneable.Clone()
        {
            return Clone();
        }

        public ArrayIndexPointer<T> Clone()
        {
            ArrayIndexPointer<T> aip = new ArrayIndexPointer<T>();
            aip._BS_Array = this._BS_Array;
            aip._BS_Index = this._BS_Index;
            return aip;
        }

        #endregion
    }
}
